#include "myTransceiver.h"


MyTransceiver::MyTransceiver()
{
    socket = new QUdpSocket(this);
    socket->bind(QHostAddress::LocalHost,1999);
    connect(socket,SIGNAL(readyRead()),this,SLOT(readData()));
}

MyTransceiver::~MyTransceiver()
{
    socket->deleteLater();
}

// Say hello to Localhost:10000
void MyTransceiver::sayHello()
{
    QByteArray msg;
    msg.append("Hello!!!");
    socket->writeDatagram(msg,QHostAddress::LocalHost,10000);
}

// Send something to Localhost:10000
void MyTransceiver::send(QByteArray msg)
{
    // "000000" is a error string
    if(msg.isEmpty())
        msg.append("000000");
    socket->writeDatagram(msg,QHostAddress::LocalHost,10000);
}

// add a "new line" to the end of the received data
void MyTransceiver::readData()
{
    buffer.resize(socket->pendingDatagramSize());
    QHostAddress sender;
    quint16 port;
    socket->readDatagram(buffer.data(),buffer.size(),&sender,&port);

    buffer.append("\n\r");

    emit newData();
}
